package com.qj.platform.pay.mssm.service;


import com.qj.base.commons.exception.CommonException;
import com.qj.base.commons.log.LoggerFactory;
import com.qj.base.commons.utils.JaxbUtil;
import com.qj.base.commons.utils.PropertiesUtil;
import com.qj.platform.pay.mssm.domain.*;
import com.qj.platform.pay.mssm.utils.MSSMPayPlatformUtil;
import feign.QueryMap;
import feign.RequestLine;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhijie on 2016/12/12.
 */
public interface MSSMPayPlatform {

    @RequestLine("POST /")
    MSSMResponse doPost(@QueryMap Map paramMap);

    default MSSMOrder queryOrder(String oriReqMsgId) throws Exception {
        MSSMOrder order=new MSSMOrder();
        order.setOriReqMsgId(oriReqMsgId);
        return queryOrder(order);
    }

    /**
     * 订单查询
     * @param body
     * @return
     * @throws Exception
     */
    default MSSMOrder queryOrder(MSSMOrder body) throws Exception {
        CommonRequest request=new QueryOrderRequest(body);
        QueryOrderResponse response=MSSMPayPlatformUtil.decryptToObj(doPost(getRequestMap("SMZF006",
                new JaxbUtil(request.getClass()).toXml(request, MSSMPayPlatformUtil.CHARSET))),QueryOrderResponse.class);
        assertSuccess(response);
        return response.getBody();
    }

    /**
     * 扫描支付，返回支付链接
     * @param body
     * @return
     * @throws Exception
     */
    default PaySMBody paySM(PaySMBody body,String orderId) throws CommonException {
        CommonRequest request=new PaySMRequest(body);
        PaySMResponse response=MSSMPayPlatformUtil.decryptToObj(doPost(getRequestMap("SMZF002",
                new JaxbUtil(request.getClass()).toXml(request,MSSMPayPlatformUtil.CHARSET),orderId)),PaySMResponse.class);
        if(!response.isSuccess()&&!"200002".equals(response.getHead().getRespCode())){
            throw new CommonException(response.getHead().getRespMsg());
        }
        return response.getBody();
    }

    default Merchant addMerchant(Merchant body) throws CommonException {
        CommonRequest request=new MerchantRequest(body);
        MerchantResponse response=MSSMPayPlatformUtil.decryptToObj(doPost(getRequestMap("SMZF001",
                new JaxbUtil(request.getClass()).toXml(request,MSSMPayPlatformUtil.CHARSET))),MerchantResponse.class);
        assertSuccess(response);
        return response.getBody();
    }
    default Merchant updateMerchant(Merchant body) throws CommonException {
        CommonRequest request=new MerchantRequest(body);
        MerchantResponse response=MSSMPayPlatformUtil.decryptToObj(doPost(getRequestMap("SMZF009",
                new JaxbUtil(request.getClass()).toXml(request,MSSMPayPlatformUtil.CHARSET))),MerchantResponse.class);
        assertSuccess(response);
        return response.getBody();
    }


    default void assertSuccess(CommonResponse response) throws CommonException {
        if(!response.isSuccess()){
            throw new CommonException(response.getHead().getRespMsg());
        }
    }
    default Map getRequestMap(String tranCode, String xml) throws CommonException {
        return getRequestMap(tranCode, xml,null);
    }

    default Map getRequestMap(String tranCode, String xml,String reqMsgId) throws CommonException {
        Map<String, String> map = new HashMap<>();
        try {
            map.put("encryptData", MSSMPayPlatformUtil.aesEncrypt(xml));
            map.put("encryptKey", MSSMPayPlatformUtil.getEncrtptKey());
            map.put("cooperator", MSSMPayPlatformUtil.getCooperator());
            map.put("signData", MSSMPayPlatformUtil.sha1(xml));
            map.put("tranCode", tranCode);
            map.put("callBack", PropertiesUtil.getProperty("mobile.baseUrl")+"/rest/order/_tradeNotify");
            if(StringUtils.isEmpty(reqMsgId)){
                reqMsgId=String.valueOf(System.currentTimeMillis());
            }
            map.put("reqMsgId", reqMsgId);
        }catch (Exception e){
            LoggerFactory.getLogger().error(e.getMessage(),e);
            throw new CommonException(e.getMessage());
        }
        return map;
    }
}
